package com.happyfamily.springboot.crowdsourcingplatform.service.sun.impl;

import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.WorkDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Work;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.WorkService;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class WorkServiceImpl implements WorkService
{
	@Autowired(required = false)
	private WorkDao workDao;

	@Override
	public List<Work> getList(Integer limit, Integer end, String uaccount)
	{
		return workDao.getList(limit,limit*(end-1),uaccount);
	}

	@Override
	public int getSum(String uaccount)
	{
		return workDao.getSum(uaccount);
	}

	@Override
	public boolean addWork(HttpServletRequest request, MultipartFile file)
	{
		boolean flag=false;
		Work work=new Work();
		try
		{
			SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			String account=securityContextImpl.getAuthentication().getName();
			work.setWuseraccount(account);
			work.setWtitle(request.getParameter("wname"));
			work.setWcontent(request.getParameter("wtext"));
			String money=request.getParameter("wmoney");
			if(money.contains("."))
			{
				money=money.substring(0,money.indexOf("."));
			}
			work.setWmoney(money);
			work.setWstarttime(new SimpleDateFormat("YYYY-MM-dd").format(new Date()));
			File f= new File("src/main/resources/doc");
			File dir=new File(f.getAbsolutePath()+"/"+account);
			if(!dir.exists())
			{
				dir.mkdir();
			}
			String name=file.getOriginalFilename();
			int index=name.lastIndexOf(".");
			String type=name.substring(index+1);
			work.setType(type);
			File newFile=new File(dir.getAbsolutePath()+"/"+name);
			int x=1;
			while(newFile.exists())
			{
				name=name.substring(0,index);
				name=name+"副本"+x+"."+type;
				newFile=new File(dir.getAbsolutePath()+"/"+name);
				x++;
			}
			work.setFname(name);
			file.transferTo(newFile);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		int n=workDao.addWork(work);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean deleteWork(Integer wid,String fname,HttpServletRequest request)
	{
		boolean flag=false;
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account=securityContextImpl.getAuthentication().getName();
		int n=workDao.deleteWork(wid);
		if(n>0)
		{
			File f= new File("src/main/resources/doc");
			File file=new File(f.getAbsolutePath()+"/"+account+"/"+fname);
			file.delete();
			flag=true;
		}
		return flag;
	}
}